package run.halo.app.model.params;

import java.util.Date;
import java.util.Set;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Site;
import run.halo.app.utils.SlugUtils;

/**
 * Site param.
 *
 * @author meazty
 * @since 2022/7/30 13:58
 **/
@Data
public class SiteParam implements InputConverter<Site> {

    private Set<Integer> tagIds;

    private Set<Integer> categoryIds;

    @NotBlank(message = "网址名称不能为空")
    @Size(max = 64, message = "网址名称不能为空 {max}")
    private String name;

    @Size(max = 64, message = "网址别名的字符长度不能超过 {max}")
    private String slug;

    @Size(max = 1023, message = "封面图链接的字符长度不能超过 {max}")
    private String cover;

    private String icon;

    private String target;

    private String url;

    private String summary;

    private String rawContent;

    private String content;

    @Min(value = 0, message = "排序字段值不能小于 {value}")
    private Integer priority = 0;

    private String remark;

    private Date createTime;

    @Override
    public Site convertTo() {
        slug = StringUtils.isBlank(slug) ? SlugUtils.slug(name) : SlugUtils.slug(slug);

        if (null == cover) {
            cover = "";
        }

        if (null == rawContent) {
            rawContent = "";
            content = "";
        }

        Site site = InputConverter.super.convertTo();
        return site;
    }

    @Override
    public void update(Site post) {
        slug = StringUtils.isBlank(slug) ? SlugUtils.slug(name) : SlugUtils.slug(slug);

        if (null == cover) {
            cover = "";
        }

        if (null == rawContent) {
            rawContent = "";
            content = "";
        }

        InputConverter.super.update(post);
    }
}
